//
//  ComicViewProgress.swift
//  MovieAppDemo
//
//  Created by yanjin on 2025/2/3.
//

import SwiftUI

struct ComicViewProgress: View {
    @Binding var showScreenProgress: Bool
    @Binding var currentIndex: Int
    @Binding var currentAll: Int
    // 获取当前时间
    @State private var currentTime: String = ""
    
    // 定时更新时间
    private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
    
    
    
    var body: some View {
        VStack {
            Spacer()
            
            HStack {
                Spacer()
                
                if showScreenProgress {
                    HStack {
                        Text("\(currentIndex+1)/\(currentAll)")
                        Text(currentTime) // 当前时间
                        Image(systemName: "battery.50percent") // 电池图标
                    }
                    .foregroundStyle(.white)
                    .padding(5)
                    .background(.black.opacity(0.3))
                    .clipShape(Capsule())
                    .padding()
                }
            }
        }
        .onReceive(timer) { _ in
            // 每秒更新当前时间
            let formatter = DateFormatter()
            formatter.dateFormat = "HH:mm" // 设置时间格式
            currentTime = formatter.string(from: Date())
        }
    }
}

#Preview {
    @Previewable @State var showScreenProgress = true
    @Previewable @State var currentIndex = 1
    @Previewable @State var currentAll = 100
    ComicViewProgress(showScreenProgress:$showScreenProgress,currentIndex: $currentIndex,currentAll: $currentAll)
}
